package com.cskaoyan.market.service.admin.impl;

import com.cskaoyan.market.db.domain.MarketKeyword;
import com.cskaoyan.market.db.domain.MarketKeywordExample;
import com.cskaoyan.market.db.mapper.MarketKeywordMapper;
import com.cskaoyan.market.service.admin.MarketKeywordService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MarketKeywordServiceImpl implements MarketKeywordService {

    @Autowired
    MarketKeywordMapper keywordMapper;

    @Override
    public List<MarketKeyword> list(Integer page, Integer limit, String id, String sort, String order, String keyword, String url, String isHot, String isDefault, String addTime, String updateTime, String sortOrder) {

        // new example
        MarketKeywordExample keywordExample = new MarketKeywordExample();
        // or 方法
        MarketKeywordExample.Criteria criteria = keywordExample.or();
        // page limit 分页使用
        // sort order 排序使用
        // id keyword url isHot isDefault addTime updateTime 条件
        // 没有传的情况下不会走到下面
        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.parseInt(id));
        }
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andKeywordEqualTo(keyword);
        }
        if (!StringUtils.isEmpty(url)) {
            criteria.andUrlEqualTo(url);
        }
        if (!StringUtils.isEmpty(isHot)) {
            criteria.andIsHotEqualTo(Boolean.parseBoolean(isHot));
        }
        if (!StringUtils.isEmpty(isDefault)) {
            criteria.andIsDefaultEqualTo(Boolean.parseBoolean(isDefault));
        }
        if (!StringUtils.isEmpty(addTime)) {
            criteria.andAddTimeEqualTo(LocalDateTime.parse(addTime));
        }
        if (!StringUtils.isEmpty(updateTime)) {
            criteria.andUpdateTimeEqualTo(LocalDateTime.parse(updateTime));
        }
        if (!StringUtils.isEmpty(sortOrder)){
            criteria.andSortOrderEqualTo(Integer.parseInt(sortOrder));
        }
        // 判断deleted
        criteria.andDeletedEqualTo(false);
        // 排序
        keywordExample.setOrderByClause(sort + " " + order);
        // 分页
        PageHelper.startPage(page, limit);
        List<MarketKeyword> marketKeywordList = keywordMapper.selectByExample(keywordExample);

        return marketKeywordList;
    }

    // 增加
    @Override
    public MarketKeyword create(MarketKeyword marketKeyword) {

        marketKeyword.setUpdateTime(LocalDateTime.now());
        marketKeyword.setAddTime(LocalDateTime.now());
        marketKeyword.setDeleted(false);
        marketKeyword.setSortOrder(1);

        keywordMapper.insert(marketKeyword);

        return marketKeyword;
    }

    // 修改
    @Override
    public int update(Integer id, String keyword, String url, boolean isHot, boolean isDefault, String addTime, String updateTime) {

        // new example
        MarketKeywordExample keywordExample = new MarketKeywordExample();
        // new marketIssue对象
        MarketKeyword marketKeyword = new MarketKeyword();
        // or 方法
        MarketKeywordExample.Criteria criteria = keywordExample.or();
        // 添加属性
        marketKeyword.setId(id);
        marketKeyword.setKeyword(keyword);
        marketKeyword.setUrl(url);
        marketKeyword.setIsHot(isHot);
        marketKeyword.setIsDefault(isDefault);

        criteria.andIdEqualTo(id);
        int row = keywordMapper.updateByExampleSelective(marketKeyword,keywordExample);

        return row;
    }

    // 删除
    @Override
    public int delete(Integer id) {
        // new example
        MarketKeywordExample keywordExample = new MarketKeywordExample();
        // new marketIssue对象
        MarketKeyword marketKeyword = new MarketKeyword();
        // or 方法
        MarketKeywordExample.Criteria criteria = keywordExample.or();

        marketKeyword.setDeleted(true);
        keywordExample.or().andIdEqualTo(id);
        int row = keywordMapper.updateByExampleSelective(marketKeyword,keywordExample);

        return row;
    }
}